package com.epoint.service;

import java.util.Calendar;
import java.util.List;

import com.epoint.dao.BorrowInfoDao;
import com.epoint.entity.BorrowInfo;

/**
 * @Description 
 * @author ylc
 */
public class BorrowInfoService {
	BorrowInfoDao borrowInfoDao = new BorrowInfoDao();
	
	/**
	 * 图书列表展示
	 * @param pageIndex
	 * @param pageSize
	 * @param sortField
	 * @param sortOrder
	 * @return
	 */
	public List<BorrowInfo> getBorrowList(int pageIndex, int pageSize,
			String sortField, String sortOrder){
		return borrowInfoDao.getBorrowList(pageIndex, pageSize, sortField, sortOrder);
	}
	/**
	 * 图书总数
	 * @return
	 */
	public int getBorrowListCount() {
		return borrowInfoDao.getBorrowListCount();
	}
	/**
	 * 获取下一个borrowId
	 * @return
	 */
	public String getNextBorrowId() {		
		Calendar calendar = Calendar.getInstance();
		// 获取当前年份
		String nowYear = Integer.toString(calendar.get(Calendar.YEAR));
		
		String nextYear = nowYear;
		// 每年序列号重新计算，从0001开始
		int nextNumber = 1;
		// 获取数据库中最大的序列号
		String maxId = getMaxBorrowId();
		if(!"".equals(maxId)) {
			// 处理最大值
			String maxYear = maxId.substring(6,10);
			// 最大年份为当前年份直接+1
			if(nowYear.equals(maxYear)) {
				nextNumber = Integer.parseInt(maxId.substring(10)) + 1;
			}// else序列号重新计算
		}
		return "Borrow" + nextYear + String.format("%04d", Integer.valueOf(nextNumber));
	}
	/**
	 * 获取最大借阅号
	 * @return
	 */
	public String getMaxBorrowId() {
		return borrowInfoDao.getMaxBorrowId();
	}
	/**
	 * 添加借阅信息
	 * @param borrowInfo
	 * @return
	 */
	public int addBorrowInfo(BorrowInfo borrowInfo) {
		return borrowInfoDao.addBorrowInfo(borrowInfo);
	}
	
	/**
	 * 更新借阅信息
	 * @param borrowInfo
	 * @return
	 */
	public int updateBorrowInfo(BorrowInfo borrowInfo) {
		return borrowInfoDao.updateBorrowInfo(borrowInfo);
	}
	/**
	 * 删除借阅信息
	 * @param borrowId
	 * @return
	 */
	public int deleteBorrowInfo(String borrowId) {
		return borrowInfoDao.deleteBorrowInfo(borrowId);
	}
	/**
	 * 判断图书是否存在借阅信息
	 * @param bookId
	 * @return
	 */
	public int isBorrowByBookId(String bookId) {
		return borrowInfoDao.isBorrowByBookId(bookId);
	}
	
}
